package servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import fulbitofacil.exception.DAOExcepcion;
import fulbitofacil.modelo.HorarioAtencion;
import fulbitofacil.modelo.Local;
import fulbitofacil.negocio.CanchaFulNegocio;

/**
 * Servlet implementation class RegistrarCanchaServlet
 */
@WebServlet("/RegistrarCanchaServlet")
public class RegistrarCanchaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegistrarCanchaServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		int idLocal = Integer.parseInt(request.getParameter("cboLocal"));
		System.out.println("idLocal "+idLocal);
		HttpSession sesion = request.getSession();
		ArrayList<HorarioAtencion> c;
		c = (ArrayList<HorarioAtencion>)sesion.getAttribute("horario");
		
		sesion.setAttribute("horario", c);

		for (int i = 0; i < c.size(); i++) {
			
			HorarioAtencion ha1 = new HorarioAtencion();
			ha1 = c.get(i);
			System.out.println("i"+i+" :"+ha1.getFecha()+"<br>");
		}

		
		Local lo = new Local();
		lo.setIdLocal(idLocal);
		String nombre = request.getParameter("txtNombre");
		System.out.println("nombre "+nombre);
		String foto = request.getParameter("file");
		System.out.println("foto "+foto);
		String caracteristicas = request.getParameter("txtCaracteristicas");
		System.out.println("caracteristicas "+caracteristicas);
		CanchaFulNegocio caFul = new CanchaFulNegocio();
		try {
			caFul.insertarCanchaFul(nombre, caracteristicas, foto, lo, c);
			
			
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		RequestDispatcher rd = request.getRequestDispatcher("RegistroCancha2.jsp");
		rd.forward(request, response);	
		
	}

}
